import { defineStore } from 'pinia'
import { nanoid } from 'nanoid'
import axios from 'axios'
import swal from 'sweetalert'

// Load组件
const useLoadStore = defineStore('load', {
    state() {
        return {
            isLoad: true, // 是否启动加载动画 true/false
        }
    }
})

// Heater组件
const useHeaterStore = defineStore('heater', {
    state() {
        return {
            userInfo: {
                avatar: './assets/images/xf.jpg', // 头像
                name: '叶晓枫', // 姓名
                profile: '我热爱你所热爱的一切!' // 介绍
            },
            navigation: {
                qq: {
                    icon: './assets/images/QQ.png', // qq图标
                    link: 'https://wpa.qq.com/msgrd?v=3&uin=1809185784&site=qq&menu=yes' // 跳转链接
                },
                wx: {
                    icon: './assets/images/wx.png', // 微信图标
                    link: './assets/upload/wx.jpg' // 弹出图片
                },
                custom: [
                    // id: 唯一标识符 (这个不用去处理) icon: 图标链接 link: 跳转链接
                    // { id: nanoid(), icon: './assets/images/wx.png', link: 'https://baidu.com' },
                ] as Array<{ id: string; link: string; icon: string }>
            },
            isDayAndNight: false
        }
    }
})

// Foote组件
const useFooteStore = defineStore('foote', {
    state() {
        return {
            title: document.title.split('-')[0],
            webUrl: location.origin,
            days: '2024-04-16', // 建站时间
            icp: '赣ICP备2020011714号' // 备案号，为空则不显示底部备案信息
        }
    },
    getters: {
        years(state) {
            const date = new Date(state.days)
            // 如果时间格式不正确就弹窗
            if (isNaN(date.getTime())) {
                swal('建站时间格式有误!', '', 'warning')
                return 'null'
            }
            const years = date.getFullYear()
            const thisYear = new Date().getFullYear()
            const n = thisYear > years ? `${years}-${thisYear}` : years
            return n
        },
        webDays(state) {
            const currentDate: Date = new Date()

            const date = new Date(state.days)
            if (isNaN(date.getTime())) {
                swal('建站时间格式有误!', '', 'warning')
                return 'null'
            }

            const targetDate: Date = new Date(state.days)

            const msPerDay: number = 24 * 60 * 60 * 1000

            const timeDiff: number = currentDate.getTime() - targetDate.getTime()

            const daysDiff: number = Math.floor(timeDiff / msPerDay)

            return daysDiff
        }
    }
})

// AinModule路由组件
const useAinModuleStore = defineStore('ainmodule', {
    state() {
        return {
            weather: { city: '加载中...', high: '加载中...' } as { city: string; high: string },
            rest: {
                title: '作息时间',
                data: [
                    { id: nanoid(), text: '09:00 起床', img: './assets/upload/get_up.png' },
                    { id: nanoid(), text: '14:00 上课', img: './assets/upload/study.png' },
                    { id: nanoid(), text: '23:00 睡觉', img: './assets/upload/sleep.png' }
                ] as Array<{ id: string; text: string; img: string }>,
            },
            fortune: {
                title: '今日运势',
                stars: '⭐⭐⭐⭐⭐',
                data: [
                    { id: nanoid(), text: '运势', width: '45%' },
                    { id: nanoid(), text: '爱情', width: '70%' },
                    { id: nanoid(), text: '健康', width: '90%' },
                    { id: nanoid(), text: '财富', width: '20%' }
                ] as Array<{ id: string; text: string; width: string }>
            },
            information: {
                title: '简介',
                img: '/assets/images/information.png',
                introduced: '欢迎使用小枫拟态UI5.0个人引导页授权版！'
            } as { title: string; img: string; introduced: string }
        }
    },
    actions: {
        async getWeather() {
            try {
                const url = 'https://api.xfyun.club/weather/' // 天气接口
                const response = await axios.get(url)
                const { code, data } = response.data
                if (code !== 200) {
                    this.weather = { city: '获取失败!', high: 'null' }
                    return
                }
                const { city, weather } = data
                this.weather = { city: city, high: weather[0].high }
            } catch (error) {
                this.weather = { city: '接口异常!', high: 'null' }
            }
        }
    }
})

// YiyanModule路由组件
const useYiyanModuleStore = defineStore('yiyanmodule', {
    state() {
        return {
            yiyan: '加载中...' as string,
            isShow: false
        }
    },
    actions: {
        async getYiyan() {
            try {
                this.yiyan = '加载中...'
                const url = 'https://api.xfyun.club/yiyan.php'
                const response = await axios.get(url)
                this.yiyan = response.data
            } catch (error) {
                this.yiyan = '接口异常!'
            }
        }
    }
})

// SiteModule路由组件
const useSiteModuleStore = defineStore('sitemodule', {
    state() {
        return {
            title: '旗下站点',
            icon: '/assets/images/site.png',
            sites: [
                { id: nanoid(), name: '小枫网络', link: 'https://www.xfabe.com', img: 'https://player.xfyun.club/img/blog_picture.jpg' },
                { id: nanoid(), name: '小枫音乐播放器', link: 'https://music.xfyun.club' , img: 'https://player.xfyun.club/img/music_picture.jpg' }
            ] as Array<{ id: string; name: string; link: string; img: string }>
        }
    }
})

// Friends路由组件
const useFriendsStore = defineStore('friends', {
    state() {
        return {
            friendsInfo: [
                { id: nanoid(), link: 'https://www.xfabe.com', name: '小枫', introduced: '拟态UI5.0开发者', qq: '1809185784' },
                { id: nanoid(), link: 'https://www.xfabe.com', name: '小枫', introduced: '拟态UI5.0开发者', qq: '1809185784' }
            ] as Array<{ id: string; link: string; name: string; introduced: string; qq: string }>
        }
    }
})

// 底部按钮名称
const useBottomTextStore = defineStore('bottomtext', {
    state() {
        return {
            homeBtnName: 'my home >',
            friendsBtnName: 'my friends >'
        }
    }
})

export {
    useLoadStore,
    useHeaterStore,
    useFooteStore,
    useAinModuleStore,
    useYiyanModuleStore,
    useSiteModuleStore,
    useFriendsStore,
    useBottomTextStore
}